package com.beverages.individual.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beverages.individual.client.SharedFilesClient;
import com.beverages.individual.common.Constants;
import com.beverages.individual.entity.Individual;
import com.beverages.individual.entity.dto.IndividualDTO;
import com.beverages.individual.entity.dto.IndividualPasswordDTO;
import com.beverages.individual.exception.ServiceException;
import com.beverages.individual.mapper.IndividualMapper;
import com.beverages.individual.service.IIndividualService;
import com.beverages.individual.utils.TokenUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class IndividualServiceImpl extends ServiceImpl<IndividualMapper, Individual> implements IIndividualService {

    private static final Log LOG = Log.get();

    @Resource
    private IndividualMapper individualMapper;

    @Autowired
    private SharedFilesClient sharedFilesClient;

    @Override
    public IndividualDTO convertIndividualEntityToIndividualDTO(Individual individual){
        IndividualDTO dto = new IndividualDTO();
        BeanUtils.copyProperties(individual, dto);

        String avatarUrl = sharedFilesClient.getFileUrlById(individual.getAvatarId()).getData();
        dto.setAvatarUrl(avatarUrl);

        String sexString;
        switch (individual.getSex()) {
            case 1:
                sexString = "男";
                break;
            case 2:
                sexString = "女";
                break;
            case 3:
                sexString = "其他";
                break;
            default:
                sexString = "未知性别";
                break;
        }
        dto.setSex(sexString);

        String roleString;
        switch (individual.getRole()) {
            case 0:
                roleString = "商家";
                break;
            case 1:
                roleString = "顾客";
                break;
            default:
                roleString = "未知角色";
                break;
        }
        dto.setRole(roleString);

        return dto;
    }

    @Override
    public IndividualDTO login(IndividualDTO individualDTO) {
        // 用户密码 md5加密
        individualDTO.setPassword(SecureUtil.md5(individualDTO.getPassword()));
        Individual one = getIndividualInfo(individualDTO);
        if (one != null) {
            IndividualDTO checkedIndividualDTO = this.convertIndividualEntityToIndividualDTO(one);
            return checkedIndividualDTO;
        } else {
            throw new ServiceException(Constants.CODE_600, "用户名或密码错误");
        }
    }

    @Override
    public Individual register(IndividualDTO individualDTO) {
        // 用户密码 md5加密
        individualDTO.setPassword(SecureUtil.md5(individualDTO.getPassword()));
        Individual one = getIndividualInfo(individualDTO);
        if (one == null) {
            one = new Individual();

            one.setUsername(individualDTO.getUsername());
            one.setPassword(individualDTO.getPassword());

            one.setAvatarId(86);
            one.setSex(1);
            one.setBalance(BigDecimal.valueOf(0));
            // 默认一个顾客的角色
            one.setRole(1);
            if (one.getNickname() == null) {
                one.setNickname(one.getUsername());
            }
            save(one);  // 把 copy完之后的用户对象存储到数据库
        } else {
            throw new ServiceException(Constants.CODE_600, "用户已存在");
        }
        return one;
    }

    @Override
    public void updatePassword(IndividualPasswordDTO individualPasswordDTO) {
        int update = individualMapper.updatePassword(individualPasswordDTO);
        if (update < 1) {
            throw new ServiceException(Constants.CODE_600, "密码错误");
        }
    }

    private Individual getIndividualInfo(IndividualDTO individualDTO) {
        QueryWrapper<Individual> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", individualDTO.getUsername());
        queryWrapper.eq("password", individualDTO.getPassword());
        Individual one;
        try {
            one = getOne(queryWrapper); // 从数据库查询用户信息
        } catch (Exception e) {
            LOG.error(e);
            throw new ServiceException(Constants.CODE_500, "系统错误");
        }
        return one;
    }



}
